package com.educate.upms.dao.impl;

import com.educate.common.mongo.QueryUtils;
import com.educate.upms.model.bo.UpmsUserOrganizationBO;
import com.educate.upms.model.po.UpmsUserOrganization;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

public class UpmsUserOrganizationDaoImpl {

    @Resource MongoTemplate mongoTemplate;

    public Page<UpmsUserOrganization> query(UpmsUserOrganizationBO bo, Pageable pageable) {
        List<Criteria> andCriteria = new ArrayList<>(10);
//        Criteria keyCriteria = QueryUtils.keyWord(keyWord, "qq", "wx", "tb");
//        if (keyCriteria != null) {
//            andCriteria.add(keyCriteria);
//        }
//        if (startTime != null || endTime != null) {
//            Criteria createTime = Criteria.where("createTime");
//            if (startTime != null) {
//                createTime.gt(startTime);
//            }
//            if (endTime != null) {
//                createTime.lt(endTime);
//            }
//            andCriteria.add(createTime);
//        }
//        if (!StringUtils.isEmpty(creatorId)) {
//            andCriteria.add(Criteria.where("creatorId").is(creatorId));
//        }
        Query query = QueryUtils.query(andCriteria).with(pageable);
        List<UpmsUserOrganization> list = mongoTemplate.find(query, UpmsUserOrganization.class);
        return PageableExecutionUtils.getPage(list, pageable, () -> mongoTemplate.count(query, UpmsUserOrganization.class));

    }
}
